<?php

class fwRender {

    private $js = array();
    private $css = array();

    function output($data, $layout=false) {
        $this->js = array_merge(S::config()->getLayout("js"), $this->js);
        $this->css = array_merge(S::config()->getLayout("css"), $this->css);
        if (class_exists("appModelLayout")
                && method_exists(M::layout(), "getMessage")
                && strlen(M::layout()->getMessage())) {
            $content = M::layout()->getMessage();
        } elseif (!isset($data["content"])) {
            if (is_array($data["content"])) {
                $content = implode("\n", $data);
            } else {
                $content = $data;
            }
        } elseif (is_array($data)) {
            extract($data);
            if (is_array($content)) {
                $content = implode("\n", $content);
            }
        } else {
            $content = $data;
        }
        if (!$layout) {
            $layout = S::controller()->getViewDir() . "layout" . DS;
            if (file_exists($layout . S::controller()->getControllerName() . ".html")) {
                $layout = $layout . S::controller()->getControllerName() . ".html";
            } else {
                $layout = $layout . "layout.html";
            }
        } else {
            $layout = $layout = S::controller()->getViewDir() . $layout . DS;
        }

        $jsf = array();
        $csf = array();
        foreach ($this->js as $jsrow) {
            $jsf[] = '<script type="text/javascript" src="/static/js/' . $jsrow . '.js"></script>';
        }
        foreach ($this->css as $cssrow) {
            $csf[] = '<link href="/static/css/' . $cssrow . '.css" rel="stylesheet" type="text/css"/>';
        }
        ob_start();
        include($layout);
        $contents = ob_get_contents();
        $contents = str_ireplace("<!--title-->", isset($title) ? $title : S::config()->getCommon("title"), $contents);
        $contents = str_ireplace("<!--javascript-->", implode("\n", $jsf), $contents);
        $contents = str_ireplace("<!--css-->", implode("\n", $csf), $contents);
        $contents = str_ireplace("<!--meta-->", implode("\n", S::config()->getCommon("meta")), $contents);
        ob_end_clean();
        // Ищем модел layout, если есть, то используем его.
        if (class_exists("appModelLayout")
                && method_exists(M::layout(), "getContent")
                && method_exists(M::layout(), "setContent")) {
            $obj = M::layout();
            $obj->setContent($contents);
            $contents = $obj->getContent($contents);
        }
        //Добавляем панель backend'a
        $backend=new fwBackend();
        $output=$backend->ouptut();
        $contents=str_ireplace("</body>", $output."</body>", $contents);
        $javascript=$backend->javascript();
        $contents=str_ireplace("</head>", "<script type='text/javascript'>\n".$javascript."\n</script>\n</head>", $contents);
        $style=$backend->style();
        $contents=str_ireplace("</head>", "<style type='text/css'>\n".$style."\n</style>\n</head>", $contents);
        unset($backend);
        
        if (S::config()->getCommon("debug"))
            return $contents;
        return preg_replace('{(\s)\1+}', '$1', trim(preg_replace('/[\r\n]+/m', "", $contents)));
    }

    public function HTML($data=array()) {
        $file = S::controller()->getViewDir() . "controller" . DS
                . S::controller()->getControllerName() . DS
                . S::controller()->getAction() . ".html";
        if (count($data)) {
            $tpl = $data[0];
            $tpl = trim(DS, dsConvert($tpl));
            $file_tpl = S::controller()->getViewDir() . $tpl . ".html";
            if (file_exists($file_tpl)) {
                $file = $file_tpl;
                $data=array_shift($data);
            }
        }
        $contents=S::cache()->html($file,$data);
        
        if (S::config()->getCommon("debug")) {
            return $contents;
        }
        return preg_replace('{(\s)\1+}', '$1', trim(preg_replace('/[\r\n]+/m', "", $contents)));
    }

}